import { _decorator, Label, Node } from 'cc';
import AudioSetting from 'db://cocos-ext/base/AudioSetting';
import { games, players, yy } from 'yycore';

import { YYPopup } from './YYPopup';

const { ccclass, property } = _decorator;

@ccclass('UISetting')
export class UISetting extends YYPopup {

    protected uiType = games.ui.UIType.GameSetting;

    // 玩家ID
    @property(Label)
    private readonly txtUID: Label = null;

    // 音乐开关
    @property(Node)
    private readonly btnNode: Node = null;

    @property(Label)
    private readonly lbVersion: Label = null;

    start() {
        // 更新玩家ID
        this.txtUID.string = players.myself.userID.toString();
        // 更新设置
        this.updateMusicSetting();

        this.lbVersion.string = yy.env.cfg.appVer || '1.0.0.0';
    }

    // 点击音效摁钮
    private onBtnClickMusic() {
        const isMute: boolean = games.setting.isMute;
        games.setting.isMute = !isMute;
        AudioSetting.mute = !isMute;
        this.updateMusicSetting();
    }

    // 刷新音效设置
    private updateMusicSetting() {
        const isOn: boolean = !games.setting.isMute;
        this.btnNode.getChildByName('btn_setting_on').active = isOn;
        this.btnNode.getChildByName('btn_setting_off').active = !isOn;
    }
}

